#include<bits/stdc++.h>
#define ONLINE_JUDGE
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 2e5 + 10, INF = 0x3f3f3f3f;

ll n, m;
pair<ll, ll> a[N];

ll find1(ll l, ll r, ll k)
{
    while (l < r) {
        ll mid = (l + r) >> 1;
        if (a[mid].first < k) l = mid + 1;
        else r = mid;
    }
    return l;
}

ll find2(ll l, ll r, ll k)
{
    while (l < r) {
        ll mid = (l + r + 1) >> 1;
        if (a[mid].first <= k) l = mid;
        else r = mid - 1;
    }
    return l;
}

void solve()
{
    cin >> n;

    a[0] = { -INF,0 };
    for (ll i = 1;i <= n;i++)
        cin >> a[i].first;
    for (ll i = 1;i <= n;i++)
        cin >> a[i].second;
    sort(a + 1, a + n + 1);
    for (ll i = 1;i <= n;i++) a[i].second += a[i - 1].second;

    cin >> m;
    while (m--) {
        ll l, r;
        cin >> l >> r;
        int L = find1(0, n, l), R = find2(0, n, r);
        if (L == 0) L = 1;
        if (a[L].first < l) L++;
        cout << a[R].second - a[L - 1].second << '\n';
    }
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    //cin>>T;
    for (ll i = 1;i <= T;i++) {
        solve();
    }
}